package cn.scw.project.goods.service.impl;

import cn.scw.common.exception.CustomException;
import cn.scw.common.utils.RegexUtils;
import cn.scw.common.utils.SecurityUtils;
import cn.scw.common.utils.StringUtils;
import cn.scw.common.utils.bean.ToolUtils;
import cn.scw.project.goods.domain.GAddress;
import cn.scw.project.goods.domain.netework.bean.GAddressAddReq;
import cn.scw.project.goods.domain.vo.GAddressVo;
import cn.scw.project.goods.mapper.GAddressMapper;
import cn.scw.project.goods.service.IGAddressService;
import cn.scw.project.goods.service.IGAreaService;
import cn.scw.project.system.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 收货地址Service业务层处理
 *
 * @author api
 * @date 2020-05-20
 */
@Service
public class GAddressServiceImpl implements IGAddressService {
    @Autowired
    private GAddressMapper gAddressMapper;
    @Autowired
    private IGAreaService igAreaService;

    /**
     * 查询收货地址
     *
     * @param id 收货地址ID
     * @return 收货地址
     */
    @Override
    public GAddress selectGAddressById(Long id) {
        return gAddressMapper.selectGAddressById(id);
    }

    @Override
    public GAddressVo selectGAddressVoById(Long id) {
        return gAddressMapper.selectGAddressVoById(id);
    }
    /**
     * 查询收货地址列表
     *
     * @return 收货地址
     */
    @Override
    public List<GAddressVo> selectGAddressList() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        return gAddressMapper.selectGAddressListByUserId(user.getUserId());
    }

    /**
     * 新增收货地址
     *
     * @param req 收货地址
     * @return 结果
     */
    @Transactional
    @Override
    public int insertGAddress(GAddressAddReq req) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        if (StringUtils.isBlank(req.getName()))
            throw new CustomException("请输入收件人姓名");
        if (StringUtils.isBlank(req.getPhone()))
            throw new CustomException("请输入收件人电话");
        if (!RegexUtils.isPhone(req.getPhone()))
            throw new CustomException("请输入正确的电话");
        if (!igAreaService.existsByCode(req.getPositionProvince()))
            throw new CustomException("地区不存在");
        if (!igAreaService.existsByCode(req.getPositionCity()))
            throw new CustomException("地区不存在");
        if (!igAreaService.existsByCode(req.getPositionArea()))
            throw new CustomException("地区不存在");
        if (StringUtils.isBlank(req.getPositionDetails()))
            throw new CustomException("请填写详细地址");
        if (req.getIsDefault()!=null&&req.getIsDefault()) {
            gAddressMapper.updateIsDefaultByUserId(sysUser.getUserId());
        } else {
            if (!gAddressMapper.existsDefaultByUserId(sysUser.getUserId()))
                req.setIsDefault(true);
        }
        GAddress gAddress = new GAddress();
        ToolUtils.copyPropertiesIgnoreNull(req, gAddress);
        gAddress.setUserId(sysUser.getUserId());

        return gAddressMapper.insertGAddress(gAddress);
    }

    /**
     * 修改收货地址
     *
     * @param req 收货地址
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateGAddress(GAddressAddReq req) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();

        GAddress gAddress = this.gAddressMapper.selectGAddressById(req.getId());
        if (gAddress == null)
            throw new CustomException("地址不存在");
        if (StringUtils.isNotBlank(req.getPhone()) && !req.getPhone().equals(gAddress.getPhone())) {
            if (!RegexUtils.isPhone(req.getPhone()))
                throw new CustomException("请输入正确的电话");
        }
        if (StringUtils.isNotBlank(req.getPositionProvince()) && !req.getPositionProvince().equals(gAddress.getPositionProvince())) {
            if (!igAreaService.existsByCode(req.getPositionProvince()))
                throw new CustomException("地区不存在");
        }
        if (StringUtils.isNotBlank(req.getPositionCity()) && !req.getPositionCity().equals(gAddress.getPositionCity())) {
            if (!igAreaService.existsByCode(req.getPositionCity()))
                throw new CustomException("地区不存在");
        }
        if (StringUtils.isNotBlank(req.getPositionArea()) && !req.getPositionArea().equals(gAddress.getPositionArea())) {
            if (!igAreaService.existsByCode(req.getPositionArea()))
                throw new CustomException("地区不存在");
        }
        if (req.getIsDefault() != null && req.getIsDefault() && !gAddress.getIsDefault())
            gAddressMapper.updateIsDefaultByUserId(sysUser.getUserId());
        else
            req.setIsDefault(null);

        ToolUtils.copyPropertiesIgnoreNull(req, gAddress);

        return gAddressMapper.updateGAddress(gAddress);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateGAddressDefault(Long id) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        gAddressMapper.updateIsDefaultByUserId(user.getUserId());
        int i = gAddressMapper.updateIsDefaultById(id);
        if (i < 1)
            throw new CustomException("修改失败");
        return i;
    }

    /**
     * 批量删除收货地址
     *
     * @param ids 需要删除的收货地址ID
     * @return 结果
     */
    @Override
    public int deleteGAddressByIds(Long[] ids) {
        return gAddressMapper.deleteGAddressByIds(ids);
    }

    /**
     * 删除收货地址信息
     *
     * @param id 收货地址ID
     * @return 结果
     */
    @Override
    public int deleteGAddressById(Long id) {
        return gAddressMapper.deleteGAddressById(id);
    }

    @Override
    public GAddressVo selectDefaultAddress() {
        SysUser user=SecurityUtils.getLoginUser().getUser();
        return gAddressMapper.selectDefaultAddress(user.getUserId());
    }
}
